<html>
  <head>
    <script
      src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
      type="application/javascript"
    ></script>
  </head>

  <body>
    <button id="connectButton" onclick="connect()">连接以太坊</button>
    <div>获取地址为</div>
    <div id="conten"></div>
    <hr />
    <button id="connectButton2" onclick="connectBaidu()">连接百度链</button>
    <div>获取地址为</div>
    <div id="conten2"></div>
    <hr />
    <button id="connectButton4" onclick="connectBaidu2()">连接百度链</button>
    <div>获取地址为</div>
    <div id="conten4"></div>
    <hr />
    <div>
      交易对象地址：<input type="text" id="to" /><br />
      交易金额：<input type="text" id="value" />
    </div>
    <button id="connectButton3" onclick="connectDemo()">以太坊交易</button>
    <div>交易哈希为</div>
    <div id="conten3"></div>
    <hr />
    <div>
      交易对象地址：<input type="text" id="toB" /><br />
      交易金额：<input type="text" id="valueB" />
    </div>
    <button id="connectButtonB" onclick="connectBaiduDemo()">百度链交易</button>
    <div>交易哈希为</div>
    <div id="contenB"></div>

    <hr />
    <button id="connectButtonB123" onclick="connectSign()">ETH钱包签名</button>
    <input type="text" id="conteninput123" />
    <div>签名sign为:</div>
    <div id="contenB123"></div>

    <hr />
    <button id="connectButtonA123" onclick="connectSignBaudu()">
      百度钱包签名
    </button>
    <input type="text" id="conteninput456" />
    <div>签名sign为:</div>
    <div id="contenA123"></div>

    <hr />
    <button id="connectButtonB456" onclick="encryptClick()">加密信息</button>
    <input type="text" id="valueCrypt" />
    <div>加密后信息为:</div>
    <div id="contenB456"></div>

    <hr />
    <button id="connectButtonB789" onclick="decryptClick()">解密信息</button>
    <input type="text" id="valueCrypt789" />
    <div>解密后信息为:</div>
    <div id="contenB789"></div>
    <hr />

    <button id="getAccounts" onclick="getAccInfo()">获取账户信息</button>
  </body>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script type="application/javascript">
    // const provider = new ethers.providers.Web3Provider(window.ethereum);
    // const accounts =  provider.send("eth_requestAccounts",[]);
    // console.log(accounts)

    // 百度交易
    async function connectBaiduDemo() {
      if (typeof window.xuper !== 'undefined') {
        try {
          const txObject = {
            to: document.getElementById('toB').value, // 接收方地址
            value: document.getElementById('valueB').value, // 转账金额
          }
          const txHash = await window.xuper.request({
            method: 'eth_sendTransaction',
            params: [
              {
                ...txObject,
                from: 'account',
              },
            ],
          })
          document.getElementById('contenB').innerHTML =
            '交易哈希值为：' + txHash
        } catch (error) {
          console.log(error)
        }
      } else {
        document.getElementById('contenB').innerHTML = '没有百度钱包'
      }
    }

    // 以太坊交易
    async function connectDemo() {
      if (typeof window.ethereum !== 'undefined') {
        try {
          const txObject = {
            to: document.getElementById('to').value, // 接收方地址
            value: document.getElementById('value').value, // 转账金额
          }
          const txHash = await window.ethereum.request({
            method: 'eth_sendTransaction',
            params: [
              {
                ...txObject,
                from: 'account',
              },
            ],
          })
          document.getElementById('conten3').innerHTML =
            '交易哈希值为：' + txHash
        } catch (error) {
          console.log(error)
        }
      } else {
        document.getElementById('conten3').innerHTML = '没有以太坊钱包'
      }
    }

    //以太坊连接
    async function connect() {
      if (typeof window.ethereum !== 'undefined') {
        try {
          let accounts = await ethereum.request({
            method: 'eth_requestAccounts',
          })
          console.log(accounts)
          document.getElementById('conten').innerHTML = accounts
        } catch (error) {
          console.log(error)
        }
        // document.getElementById("connectButton").innerHTML = "连接中";
        const accounts = await ethereum.request({
          method: 'eth_accounts',
        })
        console.log(accounts)
      } else {
        document.getElementById('conten').innerHTML = '没有以太坊钱包'
      }
    }
    //百度连接
    async function connectBaidu() {
      if (typeof window.xuper !== 'undefined') {
        try {
          let a = await xuper.request({
            method: 'eth_requestAccounts',
          })
          console.log(a)
          document.getElementById('conten2').innerHTML = a
        } catch (error) {
          console.log(error)
        }
        // document.getElementById("connectButton2").innerHTML = "连接中";
      } else {
        document.getElementById('conten2').innerHTML = '没有百度钱包'
      }
    }
    //百度连接没有eth
    async function connectBaidu2() {
      if (typeof window.xuper !== 'undefined') {
        try {
          let a = await xuper.request({
            method: 'requestAccounts',
          })
          console.log(a)
          document.getElementById('conten4').innerHTML = a
        } catch (error) {
          console.log(error)
        }
      } else {
        document.getElementById('conten4').innerHTML = '没有百度钱包'
      }
    }

    async function connectSign() {
      console.log(123124)
      if (typeof window.ethereum !== 'undefined') {
        try {
          // const txObject = {
          //   message: document.getElementById('conteninput123').value,
          // }
          // let a = await window.ethereum.request({
          //   method: 'personal_sign',
          //   params: [
          //     {
          //       ...txObject,
          //     },
          //   ],
          // })
          let a = await window.ethereum.request({
            method: 'personal_sign',
            params: [document.getElementById('conteninput123').value],
          })
          console.log(a)
          document.getElementById('contenB123').innerHTML = a
        } catch (error) {
          console.log(error)
        }
      }
    }

    async function connectSignBaudu() {
      if (typeof window.xuper !== 'undefined') {
        try {
          const txObject = {
            message: document.getElementById('conteninput456').value,
          }
          // let a = await window.xuper.request({
          //    method: "personal_sign",
          //    params: [
          //       {
          //          ...txObject
          //       }
          //    ]
          // }, 'baidu');
          let a = await window.xuper.request({
            method: 'personal_sign',
            params: [document.getElementById('conteninput456').value],
          })
          console.log(a)
          document.getElementById('contenA123').innerHTML = a
        } catch (error) {
          console.log(error)
        }
      }
    }

    async function encryptClick() {
      if (typeof window.ethereum !== 'undefined') {
        try {
          const txObject = {
            message: document.getElementById('valueCrypt').value,
          }
          let a = await window.ethereum.request({
            method: 'encryptCrypto_sign',
            params: [
              {
                ...txObject,
              },
            ],
          })
          console.log(a)
          document.getElementById('contenB456').innerHTML = a
        } catch (error) {
          console.log(error)
        }
      }
    }
    async function decryptClick() {
      if (typeof window.ethereum !== 'undefined') {
        try {
          const txObject = {
            message: document.getElementById('valueCrypt789').value,
          }
          let a = await window.ethereum.request({
            method: 'decryptCrypto_sign',
            params: [
              {
                ...txObject,
              },
            ],
          })
          console.log(a)
          document.getElementById('contenB789').innerHTML = a
        } catch (error) {
          console.log(error)
        }
      }
    }
    async function getAccInfo() {
      if (typeof window.ethereum !== 'undefined') {
        try {
          const txObject = {
            message: document.getElementById('valueCrypt789').value,
          }
          let a = await window.ethereum.request({
            method: 'getAccounts_info',
            params: [
              {
                ...txObject,
              },
            ],
          })
          console.log(a)
          document.getElementById('contenB789').innerHTML = a
        } catch (error) {
          console.log(error)
        }
      }
    }
  </script>
</html>
